package my.sample.snmp.beacon;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

import snmp.SNMPBERCodec;
import snmp.SNMPBadValueException;
import snmp.SNMPMessage;
import snmp.SNMPNull;
import snmp.SNMPObject;
import snmp.SNMPObjectIdentifier;
import snmp.SNMPPDU;
import snmp.SNMPSequence;
import snmp.SNMPVariablePair;

public class SampleGet {

	DatagramChannel dc = null;

	public SampleGet() throws IOException, SNMPBadValueException {
		
		
		
		
		
		Selector localSelector = Selector.open();
		DatagramChannel localDatagramChannel = DatagramChannel.open();
		localDatagramChannel.configureBlocking(false);
		localDatagramChannel.socket().bind(new InetSocketAddress(5555));
		localDatagramChannel.socket().setReceiveBufferSize(131071);

		localDatagramChannel.register(localSelector, 1);
		
		
		
		dc = DatagramChannel.open();
		dc.configureBlocking(false);

//		SNMPObjectIdentifier oid = new SNMPObjectIdentifier("1.3.6.1.2.1.1.1.0");
		
		
		ByteBuffer requests = ByteBuffer.allocate(100);
		
		
		SNMPSequence varList = new SNMPSequence();
		SNMPObjectIdentifier oid = new SNMPObjectIdentifier(".1.3.6.1.2.1.1.2.0");
		SNMPObject value = new SNMPNull();
		SNMPVariablePair pair = null;
		
		
		int requestId = 100;
		
		
		
		
		 varList.addSNMPObject(new SNMPVariablePair(oid, value));

		SNMPSequence pdu = new SNMPPDU(SNMPBERCodec.SNMPGETREQUEST, requestId, 0, 0, varList);

		SNMPMessage message = new SNMPMessage( 1, "SiLsnmpRO", (SNMPPDU) pdu);
		
		
		byte[] requestArray = message.getBEREncoding();
		
		 
		requests.put(requestArray);
		requests.flip();
		
		InetSocketAddress ip = new InetSocketAddress(InetAddress.getByName("202.144.0.178"), 161);
		
		dc.connect(ip);
		
		dc.write(requests);
		
		
		
		SelectionKey key = null;

	}

	public static void main(String[] args) throws IOException, SNMPBadValueException {

		SampleGet get = new SampleGet();
		get.doGet();

	}

	private void doGet() {

	}
	// private static org.apache.log4j.Logger logger =
	// org.apache.log4j.Logger.getLogger(SampleGet.class);
}
